<?php
namespace Home\Controller;
use Think\Controller;
use Home\Controller\BaseController;
class ArticleController extends BaseController
{
    public function _initialize(){
        //文章分类
        parent::_initialize();
        $article_cat = M('Article_cat')->where('show_in_nav=1')->order('sort asc')->select();
        $tree = new \Libs\Tree\Tree();
        $arr = $tree->channelLevel($article_cat, '0', '&nbsp;', 'cat_id', 'parent_id');
        $this->assign('arr',$arr);
    }

    //文章首页
    public function index(){
        //文章列表
        $data['cat_id'] = I('get.cat_id');
        $data['parent_id'] = I('get.parent_id');
        $this->assign('data',$data);
        $article = M('Article');
        if($data['cat_id']){
            $where['cat_id'] = $data['cat_id'];
            $count = $article->where($where)->count();// 查询满足要求的总记录数
            $Page = new \Think\Page($count,18);// 实例化分页类 传入总记录数和每页显示的记录数(25)
            $show       = $Page->show();// 分页显示输出
            $art = $article->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();
        }
        if(IS_POST){
            $keywords = I('post.keywords');
            $datas['title'] = array('like',"%$keywords%");
            $datas['author'] = array('like',"%$keywords%");
            $datas['_logic']  = 'or';
            $count = $article->where($datas)->count();// 查询满足要求的总记录数
            $Page = new \Think\Page($count,18);// 实例化分页类 传入总记录数和每页显示的记录数(25)
            $show       = $Page->show();// 分页显示输出
            $art = $article->where($datas)->limit($Page->firstRow.','.$Page->listRows)->select();
        }
        $this->assign('art',$art);
        $this->assign('page',$show);// 赋值分页输出

        $this->display();
    }

    //文章内容
    public function content(){
        $article_id = I('get.article_id');
        if($article_id){
            $where['article_id'] = $article_id;
            $article = M('Article');
            $content = $article->where($where)->find();
            if(!empty($content['link'])){
                if(preg_match("/(http)/i",$content['link'])){
                    redirect($content['link']);
                }else{
                    redirect(HOME_URL.$content['link']);
                }
            }
            $this->assign('content',$content);
            $lt['article_id'] = array('lt',$article_id);
            $gt['article_id'] = array('gt',$article_id);
            $front=$article->where($lt)->order('article_id desc')->find();
            $after=$article->where($gt)->order('article_id')->find();
            $this->assign('front',$front);
            $this->assign('after',$after);
        }

        $this->display();
    }



}